import '@/assets/css/asideMainAside.css'

import { useModalStore } from '@/stores/modalStore'
import { ref } from 'vue'
import { useAsyncAxios } from '@/utils/asyncFuncStore'
import type { ErrorData, UserInfo } from '@/types/loginType'
import { useRoute } from 'vue-router'

export default function () {
  // 所有数据
  const userInfo = ref<UserInfo | null>(null)

  const route = useRoute()
  const userId = route.query.id as string

  const { getUserInfo } = useAsyncAxios()

  async function userInfoInit(id: string) {
    const { status, data } = await getUserInfo(id)

    if (status === 200) {
      console.log(data)
      const result = data as UserInfo
      result.user.registAt = new Date(result.user.registAt).toLocaleString()
      const lastDate = new Date(result.user.lastAt).toLocaleDateString()
      const date = new Date().toLocaleDateString()
      if (date === lastDate) {
        result.user.lastAt = lastDate.substring(lastDate.indexOf(' '))
      } else {
        result.user.lastAt = new Date(result.user.lastAt).toLocaleString()
      }
      result.articles.map((articles) => {
        articles.dateTime = new Date(articles.dateTime).toLocaleString()
      })
      userInfo.value = result as UserInfo
    } else {
      const { changeData } = useModalStore()
      changeData({ status, data: data as ErrorData })
    }
  }

  return { userInfo, userId, userInfoInit }
}
